//
//  RansomMangerController.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/3.
//

#import "RansomMangerController.h"
#import "HandleNetProduct+Transaction.h"
#import "UIColor+NN.h"
#import "UserModel.h"
#import "MJRefresh.h"
#import "Header.h"
#import "SearchController.h"
#import "CKPuslichTool.h"
#import "RansomMangerCell.h"
#import "UIAlertView+ActionBlock.h"
#import "RansomOrderController.h"
#import "NewRansomController.h"
#import "RansomAlertController.h"
#import "ConsultantController.h"
#import "InspectController.h"

@interface RansomMangerController ()<UITableViewDelegate, UITableViewDataSource>
{
    int type;
    int count;
    NSString *searchName;
}

@property (weak, nonatomic) IBOutlet UITableView *mTableView;

@property (weak, nonatomic) IBOutlet UIView *noData;
@property (weak, nonatomic) IBOutlet UILabel *topLeft;
@property (weak, nonatomic) IBOutlet UILabel *topRight;
@property (weak, nonatomic) IBOutlet UIButton *leftBt;
@property (weak, nonatomic) IBOutlet UIButton *rightBt;
@property(strong,nonatomic)NSMutableArray *dataArray;

@end

@implementation RansomMangerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"赎回管理";
    searchName = @"";
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"sousuoW"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    type = 0;
    self.dataArray = [NSMutableArray array];
    
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        if (type == 1) {
            [self getRansomOrderList];
        }
        else {
            [self getRansomManagerList];
        }
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        if (type == 1) {
            [self getRansomOrderList];
        }
        else {
            [self getRansomManagerList];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    count = 1;
    if (type == 1) {
        [self getRansomOrderList];
    }
    else {
        [self getRansomManagerList];
    }
}

-(void)rightButton:(UIBarButtonItem *)barBt{
    //去搜索页面
    SearchController *vc=(SearchController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SearchController"];
    vc.returnSearchContent = ^(NSString *searchContent) {
        
        self.noData.hidden=YES;
        searchName = searchContent;
        if (type == 0) {
            [self getRansomManagerList];
        }else{
            [self getRansomOrderList];
        }
    };
    vc.whereFrom = @"Report";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)changType:(UIButton *)sender {
    
    if (sender.tag == 0) {
        self.topLeft.backgroundColor = [UIColor colorWithHexString:@"BB3535"];
        self.leftBt.selected = YES;
        self.topRight.backgroundColor = [UIColor clearColor];
        self.rightBt.selected = NO;
        type = 0;
        count = 1;
        searchName = @"";
        //赎回列表请求
        [self getRansomManagerList];
    }else{
        self.topRight.backgroundColor = [UIColor colorWithHexString:@"BB3535"];
        self.rightBt.selected = YES;
        self.topLeft.backgroundColor = [UIColor clearColor];
        self.leftBt.selected = NO;
        type = 1;
        count = 1;
        searchName = @"";
        //赎回订单列表请求
        [self getRansomOrderList];
    }
    [self.mTableView reloadData];
}

//赎回列表
- (void)getRansomManagerList {
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    [HandleNetProduct getRansomListWithCustomerId:mid str:searchName pageIndex:count Success:^(NSArray * _Nonnull data) {
        if (count==1) {
            [_dataArray removeAllObjects];
        }
        if (data.count > 0) {
            [_dataArray addObjectsFromArray:data];
        }
        if (_dataArray.count > 0) {
            _noData.hidden = YES;
        }else{
            _noData.hidden = NO;
        }
        [_mTableView reloadData];
        
        if (_dataArray.count < 20) {
            _mTableView.mj_footer.hidden = YES;
        }
        else {
            _mTableView.mj_footer.hidden = NO;
        }
        
        [_mTableView.mj_header endRefreshing];
        [_mTableView.mj_footer endRefreshing];
    } Failure:^(NSString * _Nonnull errorInfo) {
        [_mTableView.mj_header endRefreshing];
        [_mTableView.mj_footer endRefreshing];
    }];
}

//赎回订单列表
- (void)getRansomOrderList {
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    [HandleNetProduct getRansomOrderListWithCustomerId:mid str:searchName pageIndex:count Success:^(NSArray * _Nonnull data) {
        if (count==1) {
            [_dataArray removeAllObjects];
        }
        if (data.count > 0) {
            [_dataArray addObjectsFromArray:data];
        }
        if (_dataArray.count > 0) {
            _noData.hidden = YES;
        }else{
            _noData.hidden = NO;
        }
        [_mTableView reloadData];
        
        if (_dataArray.count < 20) {
            _mTableView.mj_footer.hidden = YES;
        }
        else {
            _mTableView.mj_footer.hidden = NO;
        }
        
        [_mTableView.mj_header endRefreshing];
        [_mTableView.mj_footer endRefreshing];
    } Failure:^(NSString * _Nonnull errorInfo) {
        [_mTableView.mj_header endRefreshing];
        [_mTableView.mj_footer endRefreshing];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (type == 0) {
        return 160;
    }
    else {
        return 110;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (type == 0) {
        RansomMangerCell *cell=[tableView dequeueReusableCellWithIdentifier:@"RansomMangerCell1" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        [cell.ransomBtn addTarget:self action:@selector(clickRansomBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.ransomBtn.tag = indexPath.section;
        
        RansomModel *model = [RansomModel mj_objectWithKeyValues:_dataArray[indexPath.section]];
        [cell setRansomManagerData:model];
        
        return cell;
    }else{
        RansomMangerCell *cell=[tableView dequeueReusableCellWithIdentifier:@"RansomMangerCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        
        RansomOrderModel *model = [RansomOrderModel mj_objectWithKeyValues:_dataArray[indexPath.section]];
        [cell setRansomOrderData:model];
        
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (type == 1) {
        RansomOrderController *vc = (RansomOrderController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomOrderController"];
        RansomOrderModel *model = [RansomOrderModel mj_objectWithKeyValues:_dataArray[indexPath.section]];
        vc.redeemId = model.RedeemId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)clickRansomBtnAction: (UIButton *)sender {
    RansomModel *model = [RansomModel mj_objectWithKeyValues:_dataArray[sender.tag]];
    if ([model.IsRedeem integerValue] == 1) {
        RansomAlertController *vc = (RansomAlertController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomAlertController"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
        vc.isRansom = YES;
        vc.returnBlock = ^(NSInteger type) {
            switch (type) {
                case 2:
                {
                    NewRansomController *vc = (NewRansomController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"NewRansomController"];
                    vc.contractNo = model.ContractNo;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
            }
        };
        [self presentViewController:vc animated:YES completion:nil];
    }
    else {
        RansomAlertController *vc = (RansomAlertController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomAlertController"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
        vc.yuyueText = model.RedeemMessage;
        vc.returnBlock = ^(NSInteger type) {
            switch (type) {
                case 1:
                {
                    InspectController *vc=(InspectController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"InspectController"];
                    vc.productId = model.ProductID;
                    vc.customerID = model.CustomerID;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:
                    {
                        ConsultantController *vc = (ConsultantController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"ConsultantController"];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    break;
            }
        };
        [self presentViewController:vc animated:YES completion:nil];
    }
}

@end
